package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.common.Cache;
import com.southwind.common.ResultObj;
import com.southwind.common.WorkPageModel;
import com.southwind.entity.Admin;
import com.southwind.entity.AfterSalesMessage;
import com.southwind.entity.Work;
import com.southwind.entity.Worker;
import com.southwind.mapper.*;
import com.southwind.service.WorkService;
import com.southwind.vo.AfterSalesVO;
import com.southwind.vo.WorkVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/work")
public class WorkController {

    @Autowired
    private WorkerMapper workerMapper;
    @Autowired
    private StationMapper stationMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private WorkService workService;
    @Autowired
    private WorkMapper workMapper;

    @PostMapping("/add")
    public ResultObj add(@RequestBody Work work){
        Admin admin = (Admin) Cache.map.get("user");
        work.setAid(admin.getId());
        work.setDate(new Date());
        work.setStatus("未完成");
        Integer worker = this.workerMapper.updateStatus("忙碌", work.getWid());
        Integer station = this.stationMapper.updateStatus("忙碌", work.getSid());
        Integer car = this.carMapper.updateStatus("清洗中", work.getCid());
        boolean save = this.workService.save(work);
        if(save && worker == 1 && station == 1 && car == 1) return ResultObj.WORK_ADD_SUCCESS;
        return ResultObj.WORK_ADD_ERROR;
    }

    @GetMapping("/list")
    public Map list(WorkPageModel workPageModel){
        Page<Work> page = new Page<>(workPageModel.getPage(), workPageModel.getLimit());
        Worker worker = (Worker) Cache.map.get("user");
        QueryWrapper<Work> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wid", worker.getId());
        Page<Work> resultPage = this.workService.page(page,queryWrapper);
        Map map = new HashMap();
        map.put("count", resultPage.getTotal());
        List<WorkVO> list = new ArrayList<>();
        for (Work record : resultPage.getRecords()) {
            WorkVO workVO = new WorkVO();
            BeanUtils.copyProperties(record, workVO);
            workVO.setAdmin(this.adminMapper.getNameById(record.getAid()));
            workVO.setNumber(this.carMapper.getCarNumberById(record.getCid()));
            workVO.setStation(this.stationMapper.getNameById(record.getSid()));
            workVO.setWorker(this.workerMapper.getNameById(record.getWid()));
            list.add(workVO);
        }
        map.put("data", list);
        return map;
    }

    @DeleteMapping("/finish/{id}")
    public ResultObj finish(@PathVariable("id") Integer id){
        Integer finish = this.workMapper.finish(id);
        Work work = this.workService.getById(id);
        Integer worker = this.workerMapper.updateStatus("空闲",work.getWid());
        Integer car = this.carMapper.updateStatus("洗车完毕", work.getCid());
        Integer station = this.stationMapper.updateStatus("空闲", work.getSid());
        if(finish == 1 && worker == 1 && car == 1 && station == 1) return ResultObj.WORK_FINISH_SUCCESS;
        return ResultObj.WORK_FINISH_ERROR;
    }
}

